<template>
  <div class="container">
    <div class="app-container">
      <el-button class="btn-add" size="mini" type="primary" @click="openDia(0,1)">添加权限</el-button>

      <el-table default-expand-all border :data="list" row-key="id">
        <el-table-column prop="name" label="名称" />
        <el-table-column prop="code" label="标识" />
        <el-table-column prop="description" label="描述" />
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <el-button v-if="row.type === 1" size="mini" type="text" @click="openDia(row.id,2)">添加</el-button>
            <el-button size="mini" type="text" @click="openDetail(row.id)">编辑</el-button>
            <el-button size="mini" type="text" @click="del(row.id)">删除</el-button>
          </template></el-table-column>
      </el-table>
      <el-dialog
        :title="`${showText}权限点`"
        :visible.sync="dialogVisible"
        width="38%"
        @close="onCancel"
      >
        <el-form ref="formRef" :model="formData" :rules="rules">
          <el-form-item label="权限名称" label-width="120px" prop="name">
            <el-input v-model="formData.name" style="width:80%" />
          </el-form-item>
          <el-form-item label="权限标识" label-width="120px" prop="code">
            <el-input v-model="formData.code" style="width:80%" />
          </el-form-item>
          <el-form-item label="权限描述" label-width="120px" prop="description">
            <el-input v-model="formData.description" type="textarea " style="width:80%" />
          </el-form-item>
          <el-form-item label="开启" label-width="120px" prop="enVisible">
            <el-switch
              v-model="formData.enVisible"
              :active-value="'1'"
              :inactive-value="'0'"
              active-color="#13ce66"
              inactive-color="#dce0e6"
            />
          </el-form-item>
          <el-form-item style="text-align:center">
            <el-button type="primary" @click="onSubit">确定</el-button>
            <el-button @click="onCancel">取消</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
    </div>

  </div>

</template>

<script>
import { addPermission, delPermission, getPermissionDetail, getPermissionListApi, updatePermission } from '@/api/permission'
import { transObjToTree } from '@/utils'

export default {
  name: 'Permission',
  data() {
    return {
      list: [],
      dialogVisible: false,
      formData: {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        type: '', // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
        pid: '', // 因为做的是树 需要知道添加到哪个节点下了
        enVisible: 0 // 默认关闭
      },
      rules: {
        name: [{ required: true, message: '权限名称不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    showText() {
      return this.formData.id ? '编辑' : '新增'
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async  getPermissionList() {
      this.list = transObjToTree(await getPermissionListApi(), 0)
    },
    del(id) {
      this.$confirm('此操作将永久删除该权限, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delPermission(id)
        this.getPermissionList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
      })
    },
    openDia(pid, type) {
      this.dialogVisible = true
      this.formData.type = type
      this.formData.pid = pid
    },
    async openDetail(id) {
      this.dialogVisible = true
      const res = await getPermissionDetail(id)
      this.formData = res
    },
    onSubit() {
      this.$refs.formRef.validate(async(e) => {
        if (this.formData.update_time) {
          await updatePermission(this.formData)
          this.$message.success('更新权限成功')
        } else {
          await addPermission(this.formData)
          this.$message.success('新增权限成功')
        }
        this.onCancel()
        this.getPermissionList()
      })
    },
    onCancel() {
      this.$refs.formRef.resetFields()
      this.formData = {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        type: '', // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
        pid: '', // 因为做的是树 需要知道添加到哪个节点下了
        enVisible: '0' // 默认关闭
      }
      this.dialogVisible = false
    }
  }
}
</script>

<style>
.app-container{
  box-sizing: border-box;
  padding: 18px;
}
.btn-add {
  margin: 10px;
}
</style>
